$(document).ready(function () {
    var path = '<%=request.getContextPath()%>';
    var date = new Date();
    var nowDate = date.toLocaleDateString();
    var fullDate = date.toLocaleString();
    var questionId = null;
    var teamExamId = null;

    $.ajax({
        type: "get",
        url: "/todayTeamExam/checkHomework",
        data: {
            date: nowDate
        },
        dataType: "json",
        success: function (data) {
            if (data == null) {
                alert("今日没有作业！");
            }
            if (data != null) {
                questionId = data.questionId;
                teamExamId = data.id;
               // alert("检查是不是第一次进入：");
              checkFirstLogIn();
                showQuestion();
               // initSelfExamTransaction();
            }

        }

    })

    function checkFirstLogIn() {
        var teamExamTransaction = {
            teamExamId: teamExamId
        }
        $.ajax({
            type: "get",
            url: "/teamExamTransaction/checkFirstLogIn",
            data: teamExamTransaction,
            dataType: "json",
            success: function (data) {
                if (data === null) {
                  //  alert("第一次进入！");
                    initSelfExamTransaction();
                    showQuestion();
                    time_now();
                    time_fun();

                } else if(data.answerUrl!=null){
                    var div = document.getElementById("contentwrapper");
                    div.style.display = "none";
                    var div = document.getElementById("divSub");
                    div.style.display = "none";
                    var div = document.getElementById("divMessage");
                    div.style.display = "block";
                }else {
                   alert("第二次进入！");
                    showQuestion();
                    time_now2();
                    time_fun2();
                }
            }
        })
    }

    function showQuestion() {
        $.ajax({
            type: "post",
            url: "/todayTeamExam/getQuestionContent",
            data: {questionId: questionId},
            dataType: "text",
            success: function (data) {
                var questions = document.getElementById("questions");
                var div = document.getElementById("divSub");
                div.style.display = "block";
                questions.value = data;

            }
        })
    }

    function time_now() {
        var dNow = new Date();
        document.getElementById("date").innerHTML = "开始时间:" + two_char(dNow.getHours()) + ":" + two_char(dNow.getMinutes()) + ":" + two_char(dNow.getSeconds());
    }

    function time_now2() {
        $.ajax({
            type: "get",
            data: {
                teamExamId: teamExamId
            },
            url: "/teamExamTransaction/getStartTime",
            dataType: "text",
            success: function (data) {
                document.getElementById("date").innerHTML = "开始时间:" + data.substring(11, 19);
            }
        })

    }

    function time_fun() {
        var sec = 0;
        setInterval(function () {
            sec++;
            var date = new Date(0, 0)
            date.setSeconds(sec);
            var h = date.getHours(), m = date.getMinutes(), s = date.getSeconds();
            document.getElementById("mytime").innerText = "已用时间:" + two_char(h) + ":" + two_char(m) + ":" + two_char(s);
        }, 1000);
    }

    function time_fun2() {
        var time1;
        $.ajax({
            type: "get",
            data: {
                teamExamId: teamExamId
            },
            async: false,
            url: "/teamExamTransaction/getStartTime",
            dataType: "text",
            success: function (data) {
                time1 = data.substring(0, 19);
            }
        })
        var sec = 0;
        setInterval(function () {
            sec++;
            var date2 = new Date();
            date2.setSeconds(sec);
            var year1 = time1.substring(0, 4), month1 = time1.substring(5, 7), day1 = time1.substring(8, 10);
            var h1 = time1.substring(11, 13), m1 = time1.substring(14, 16), s1 = time1.substring(17, 19);
            var date1 = new Date();
           // date1.setFullYear(parseInt(year1), parseInt(month1), parseInt(day1));
            date1.setHours(parseInt(h1));
            date1.setMinutes(parseInt(m1));
            date1.setSeconds(parseInt(s1));
            spendTime(date1, date2);
            var h = date2.getHours() - h1, m = date2.getMinutes() - m1, s = date2.getSeconds() - s1;
        }, 1000);
    }

    function spendTime(date1, date2) {
        var date3 = date2.getTime() - date1.getTime()  //时间差的毫秒数


//计算出相差天数
        var days = Math.floor(date3 / (24 * 3600 * 1000))

//计算出小时数

        var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
        var hours = Math.floor(leave1 / (3600 * 1000))
//计算相差分钟数
        var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
        var minutes = Math.floor(leave2 / (60 * 1000))


//计算相差秒数
        var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
        var seconds = Math.round(leave3 / 1000)


      //  alert(" 相差 " + days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒")
        document.getElementById("mytime").innerText = "已用时间:" + two_char(hours) + ":" + two_char(minutes) + ":" + two_char(seconds);

    }

    function two_char(n) {
        return n >= 10 ? n : "0" + n;
    }

    function initSelfExamTransaction() {
        var teamExamTransaction = {
            teamExamId: teamExamId,
            startTime: fullDate
        }
        $.ajax({
            type: "get",
            url: "/teamExamTransaction/initTeamExamTransaction",
            data: teamExamTransaction,
            dataType: "text",
            success: function (data) {
                if (data != "") {
                    alert("请开始答题！");
                }
            }
        })
    }

    $("#file1").click(function () {
        alert("注意：\n 作业提交后不能修改，请仔细检查后提交！\n (仅支持以.zip格式提交的压缩包！)")
    })

    $("#submitHomeWork").click(function () {
        var form = new FormData(document.getElementById("tf"));

        saveLists();
        var div = document.getElementById("contentwrapper");
        div.style.display = "none";
        var div = document.getElementById("divSub");
        div.style.display = "none";
        var div = document.getElementById("divMessage");
        div.style.display = "block";

        function saveLists() {
            $.ajax({
                type: "post",
                url: "/teamUpload",
                data: form,
                dataType: "text",
                processData:false,
                contentType:false,
                async:"false",
                success:function(data){
                    updateUrlSpendTime();

                    console.log("over..");
                },
                error:function(e){
                    alert("上传错误！");
                }
            })
        }
        
        function updateUrlSpendTime() {
            var myTime1= $("#mytime").text();
            var myTime2=myTime1.split(":");
            var myTime=myTime2[1]+":"+myTime2[2]+":"+myTime2[3];
            var teamExamTransaction={
                teamExamId:teamExamId,
                spendTime:myTime
            }
            $.ajax({
                type:"post",
                url:"/teamExamTransaction/updateUrlSpendTime",
                data:teamExamTransaction,
                dataType:"text",
                async:"false",
                success:function (data) {
                    alert("上传成功！")
                }
          })

        }

    })


})
